package com.creative.accountsdk.holder;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.creative.accountsdk.AccountActivity;
import com.creative.accountsdk.AccountSdk;
import com.creative.accountsdk.AccountUtils;
import com.creative.accountsdk.AuthorizeListener;
import com.creative.accountsdk.DataCenter;
import com.creative.accountsdk.DialogUtils;
import com.creative.accountsdk.OnLoginListener;
import com.creative.accountsdk.R;
import com.creative.accountsdk.UserData;
import com.creative.accountsdk.net.XHttpHandler;
import com.creative.accountsdk.net.account.LoginProtocol;
import com.creative.accountsdk.net.account.ThirdLoginProtocol;
import com.creative.accountsdk.widget.LineEditText;

import  cz.msebera.android.httpclient.Header;
import org.json.JSONObject;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformDb;

/**
 * Created by yinjialiang on 15-6-23.
 */
public class LoginHolder extends BaseHolder{
    //private Dialog progress;
    private EditText  etUserName,etUserPwd;
    private View buttonFindPassword, layoutThird, buttonOtherLogin;
    private View buttonWeibo, buttonWeixin, buttonQQ, buttonFacebook;
    private Button buttonOK;
    private final static String TAG = LoginHolder.class.getSimpleName();

    @Override
    protected void initViews() {
        viewRoot.findViewById(R.id.buttonRegister).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activity.hideSoftInput();
                activity.popupHolder();
                activity.showHolder(new RegisteHolder());
            }
        });
        etUserName = (EditText)viewRoot.findViewById(R.id.et_login_name);

        etUserPwd = (EditText)viewRoot.findViewById(R.id.et_login_pwd);
        buttonOK = (Button)viewRoot.findViewById(R.id.buttonOK);
       // buttonOK.setEnabled(false);
        buttonOK.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                login();
            }
        });

        buttonWeibo = viewRoot.findViewById(R.id.buttonWeibo);
        buttonWeixin = viewRoot.findViewById(R.id.buttonWeixin);
        buttonQQ = viewRoot.findViewById(R.id.buttonQQ);
        buttonFacebook = viewRoot.findViewById(R.id.buttonFacebook);
        buttonWeibo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showAuthorizeDialog();
                activity.weiboLogin(LoginHolder.this);
            }
        });
        buttonWeixin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showAuthorizeDialog();
                activity.weixinLogin(LoginHolder.this);
            }
        });
        buttonQQ.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showAuthorizeDialog();
                activity.qqLogin(LoginHolder.this);
            }
        });
        buttonFacebook.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showAuthorizeDialog();
                activity.facebookLogin(LoginHolder.this);
            }
        });
        buttonOtherLogin = viewRoot.findViewById(R.id.buttonOtherLogin);
        buttonOtherLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DataCenter.switchThirdLogin();
            }
        });

        buttonFindPassword = viewRoot.findViewById(R.id.buttonFindPassword);
        buttonFindPassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                findPassword();
            }
        });
        buttonFindPassword.setVisibility(View.VISIBLE);
        layoutThird = viewRoot.findViewById(R.id.layoutThird);

        //text watcher
        TextWatcher textWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(etUserName.getText().toString().equals("") || etUserPwd.getText().toString().equals(""))
                {
                  //  buttonOK.setEnabled(false);
                }
                else
                {
                  //  buttonOK.setEnabled(true);
                }
            }
        };
        etUserName.addTextChangedListener(textWatcher);
        etUserPwd.addTextChangedListener(textWatcher);
    }



    private void findPassword()
    {
        Log.d(TAG, "findPassword");
        activity.hideHolder();
        activity.showHolder(new FindPasswordHolder());
    }

    private void login()
    {
        String username = etUserName.getText().toString().trim();
        String password = etUserPwd.getText().toString().trim();
        if (username.equals("")) {
            Toast.makeText(activity, R.string.account_username_empty, Toast.LENGTH_SHORT).show();
        }
        else if (password.equals("")) {
            Toast.makeText(activity, R.string.account_password_empty, Toast.LENGTH_SHORT).show();
        } else {
            progress.show();
            final LoginProtocol protocol = new LoginProtocol(username, password);
            protocol.debugUrl(DataCenter.getHttpClient());
            DataCenter.getHttpClient().post(protocol.getUrl(), protocol.getParams(), new XHttpHandler(activity, progress) {
                @Override
                public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                    super.onSuccess(statusCode, headers, response);
                    progress.cancel();
                    Log.d(TAG,"登陆返回数据:"+response);
                    if (protocol.parse(response, activity)) {
                        Log.d(TAG, "login success");
                        UserData.getInstance().init(protocol.jsonUser);
                        UserData.getInstance().write(activity);


                        if (UserData.getInstance().getRole() == 0) {
                            toSetRole();
                        } else {
                            activity.finish();
                            OnLoginListener loginListener = AccountSdk.getInstance().getLoginListener();
                            if (loginListener != null) {
                                loginListener.onComplete(protocol.jsonUser);
                            }
                        }
                    }
                }
            });
        }
    }

    @Override
    public int getLayoutResID() {
        return R.layout.account_login;
    }

    @Override
    public void resizeBigger() {
        //buttonFindPassword.setVisibility(View.GONE);
        layoutThird.setVisibility(View.VISIBLE);
        buttonOtherLogin.setVisibility(View.VISIBLE);
    }

    @Override
    public void resizeSmaller() {
      //  buttonFindPassword.setVisibility(View.VISIBLE);
        layoutThird.setVisibility(View.GONE);
        buttonOtherLogin.setVisibility(View.GONE);
    }

    private void toSetRole() {
        activity.hideHolder();
        activity.showHolder(new SelectIdentityHolder());
    }
}
